#include "stc8h.h"
#include "../main/configuration.h"
#include "../LED/LED.h"

/*#############宏定义#############*/








/***##############变量定义####################*/


unsigned char data _tenth_dig=_dig_off_;

unsigned char data _dig=_dig_off_;

unsigned char data shumaguan_shi_shanshuo_en=0;               //闪烁使能

unsigned char data shumaguan_ge_shanshuo_en=0;                //闪烁使能

//unsigned char shumaguan_pwm_count=0;

//unsigned char shumaguan_refresh_en=0;

//unsigned int shumaguan_shi_shanshuo_count=0;        //十位闪烁计数

//unsigned int shumaguan_ge_shanshuo_count=0;         //各位闪烁计数

unsigned int data shumaguan_shanshuo=0;             //闪烁间隔，用于切换快闪慢闪

unsigned char data _brightness;                    //数码管亮度变量

/******************************************************/

const unsigned char code _brightness_pwm[]=                     //数码管亮度-PWM对照表
{
    0,  //off
    1,  //1
    2,  //2
    4,  //3
    6,  //4
    10  //5
};


const unsigned char code _shumaguan_gewei[]=                             //个位数码管字表（共阴）
{
    0xEE ,  //0/O
    0x28 ,  //1
    0xCD ,  //2
    0x6D ,  //3
    0x2B ,  //4
    0x67 ,  //5
    0xE7 ,  //6
    0x2C ,  //7
    0xEF ,  //8
    0x6F ,  //9
    0x87 ,  //F     10
    0xC2 ,  //L     11
    0xAE ,  //n     12
    0xEA ,  //U     13
    0x1F ,  //P     14
    0xE9 ,  //d     15
    0x00    //关闭显示  16
};

const unsigned char code _shumaguan_shiwei[]=                             //十位数码管字表（共阴）
{
    0x77 ,  //0/O
    0x41 ,  //1
    0x3B ,  //2
    0x6B ,  //3
    0x4D ,  //4
    0x6E ,  //5
    0x7E ,  //6
    0x43 ,  //7
    0x7F ,  //8
    0x6F ,  //9
    0x1E ,  //F     10
    0x34 ,  //L     11
    0x57 ,  //n     12
    0x75 ,  //U     13
    0x1F ,  //P     14
    0x79 ,  //d     15
    0x00    //关闭显示  16
};

void display(unsigned char data _tenth, unsigned char data _ddig)     //0xff等于维持原样                      
{
    if(_tenth<0xff){_tenth_dig=~_shumaguan_shiwei[_tenth];}
    if(_ddig<0xff){_dig=~_shumaguan_gewei[_ddig];}

}


void display_init()                     //显示初始化
{
    P1M0 |= 0x0c;                   //P12 P13推挽输出
//    P3M0 |= 0xFF;                   //P3 推挽输出


    shumaguan_shi_shanshuo_en=0;
    shumaguan_ge_shanshuo_en=0;

    display(_dig_off_,_dig_off_);
    _display_on=0;
    _brightness=settings.f4.brightness;
    Timer0_Init();
}



void display_driver()                                               //显示驱动
{
    static unsigned char data shumaguan_pwm_count=0;
    static unsigned char data shumaguan_refresh_en=0;
    static unsigned int data shumaguan_shi_shanshuo_count=0;
    static unsigned int data shumaguan_ge_shanshuo_count=0;
    
 //   if(!_display_on){shumaguan_shanshuo=0;}                              //后添加，用于关闭显示
//    if(!shumaguan_shanshuo){shumaguan_shanshuo_count=0;shumaguan_shanshuo=2;}              //后添加，用于shumaguan_shanshuo为零时关闭闪烁
    switch (shumaguan_refresh_en)
    {
    case 0:
        _sd=0;

        if ((shumaguan_shi_shanshuo_count<shumaguan_shanshuo)||(!shumaguan_shi_shanshuo_en))            //数码管闪烁时间判断
        {
        
            if (shumaguan_pwm_count<_brightness_pwm[_brightness]&&_display_on)                    //数码管亮度判断
            {
            
                P3=_tenth_dig;
                _td=1;
            }else{
                _td=0;
            }
            break;
        
        }else{
            _td=0;
            
        }
        break;

    case 1:
        _td=0;
        
        if ((shumaguan_ge_shanshuo_count<shumaguan_shanshuo)||(!shumaguan_ge_shanshuo_en))            //数码管闪烁时间判断
        {
        
            if (shumaguan_pwm_count<_brightness_pwm[_brightness]&&_display_on)                    //数码管亮度判断
            {
            
                P3=_dig;
                _sd=1;
                
            
            }else{
                _sd=0;
                
            }
            
        
        }else{
            _sd=0;
            
        }
        break;

    }
    switch (++shumaguan_pwm_count>10)
    {
    case 1:
        shumaguan_pwm_count=0;
        switch(shumaguan_refresh_en)
        {
        case 0:
            switch (++shumaguan_shi_shanshuo_count<shumaguan_shanshuo*2)            //判断闪烁周期
            {
            case 1:
                
                break;
            
            case 0:
                shumaguan_shi_shanshuo_count=0;
                break;
            }
            break;
        case 1:
            switch (++shumaguan_ge_shanshuo_count<shumaguan_shanshuo*2)            //判断闪烁周期
            {
            case 1:
                
                break;
            
            case 0:
                shumaguan_ge_shanshuo_count=0;
                break;
            }
            break;
        }
    shumaguan_refresh_en=!shumaguan_refresh_en;

    case 0:
        break;
    }

}



